// app/controller/news.js
const { Controller } = require('egg');

class ArticleController extends Controller {
  async createArticle() {
    const { ctx, service } = this
    const { article } = ctx.request.body;

    console.log(article)

    const rule = {
        title: { type: 'string', required: false,message: '标题不能为空', },
        description: { type: 'string', required: false, message: '描述不能为空', },
        body: { type: 'string', required: false, message: '内容不能为空', },
        tagList: { type: 'array', required: false, message: '标签不能为空',}
    };
  
    if(!article) {
        ctx.throw(ctx.errorMap.paramsError.code, 'article 参数不能为空');
    }
    const { title, description, body, tagList } = article
    if (!title && !description && !body && !tagList) {
      ctx.throw(ctx.errorMap.paramsError.code, 'title, description, body, tagList 不能全为空');
    }

    ctx.validate(rule);

    const result = await service.article.createArticle();
    // 判断更新是否成功
    if(result) {
      ctx.body = ctx.showSuccess(result)
    } else {
      ctx.body = {
        code: ctx.errorMap.createArticleFailed.code,
        message: ctx.errorMap.createArticleFailed.message,
      }
    }
  }
  async listArticles() {
    const { ctx, service } = this
    const { tag, author, favorited, limit, offset } = ctx.query;
    console.log(tag, author, favorited, limit, offset)

    const listRule = {
      tag: { type: 'string', required: false },
      author: { type: 'string', required: false },
      favorited: { type: 'string', required: false },
      limit: { type: 'int', required: false, min: 1, max: 100 },
      offset: { type: 'int', required: false, min: 0 },
    };

    ctx.validate(listRule);


    let result = await service.article.listArticles()
    if(result) {
      ctx.body = ctx.showSuccess(result)
    } else {
      ctx.body = {
        code: ctx.errorMap.queryError.code,
        message: ctx.errorMap.queryError.message,
      }
    }
  }
  async getArticle() {
    const { ctx, service } = this
    console.log('service getArticle',  ctx.params)
    const rule = {
      slug: { type: 'string', required: false }
    };
    ctx.validate(rule);
    let result = await service.article.getArticle()
    if(result) {
      ctx.body = ctx.showSuccess(result)
    } else {
      ctx.body = {
        code: ctx.errorMap.queryError.code,
        message: ctx.errorMap.queryError.message,
      }
    }
  }
  async updateArticle() {
    const { ctx, service } = this
    const { article } = ctx.request.body;
    console.log('updateArticle',  ctx.params)
    const rule = {
        title: { type: 'string', required: false,message: '标题不能为空', },
        description: { type: 'string', required: false, message: '描述不能为空', },
        body: { type: 'string', required: false, message: '内容不能为空', }
    };

    if(!article) {
        ctx.throw(ctx.errorMap.paramsError.code, 'article 参数不能为空');
    }
    const { title, description, body } = article
    if (!title && !description && !body) {
        ctx.throw(ctx.errorMap.paramsError.code, 'title, description, body 不能全为空');
    }
    ctx.validate(rule);
    let result = await service.article.updateArticle()
    if(result.ok === 1 && result.nModified === 1) {
      ctx.body = ctx.showSuccess(result)
    } else {
      ctx.body = {
        code: ctx.errorMap.updateArticleFail.code,
        message: ctx.errorMap.updateArticleFail.message,
      }
    }
  }
  async deleteArticle() {
    const { ctx, service } = this
    console.log('deleteArticle',  ctx.params)
    const rule = {
      slug: { type: 'string', required: false }
    };
    ctx.validate(rule);
    let result = await service.article.deleteArticle()
    if(result) {
      ctx.body = ctx.showSuccess(result)
    } else {
      ctx.body = {
        code: ctx.errorMap.deleteArticleFail.code,
        message: ctx.errorMap.deleteArticleFail.message,
      }
    }
  }
  async feedArticles() {
    const { ctx, service } = this
    console.log(ctx.query,'|', ctx.params)

    const listRule = {
      limit: { type: 'int', required: false, min: 1, max: 100 },
      offset: { type: 'int', required: false, min: 0 },
    };

    ctx.validate(listRule);

    let result = await service.article.feedArticles()
    if(result) {
      ctx.body = ctx.showSuccess(result)
    } else {
      ctx.body = {
        code: ctx.errorMap.queryError.code,
        message: ctx.errorMap.queryError.message,
      }
    }
  }
  async addComments() {
    const { ctx, service } = this
    const { slug } = ctx.params
    const { comment } = ctx.request.body;
    const user = ctx.state.user
    if(!comment) ctx.throw(ctx.errorMap.paramsError.code, 'comment 参数不能为空');
    const { body } = comment
    if(!body) ctx.throw(ctx.errorMap.paramsError.code, 'body 参数不能为空');

    const rule = {
      slug: { type: 'string', required: false }
    };
    ctx.validate(rule);
    const result = await service.comment.addComments()
    if(result) {
      ctx.body = ctx.showSuccess(result)
    } else {
      ctx.body = {
        code: ctx.errorMap.createCommentFail.code,
        message: ctx.errorMap.createCommentFail.message,
      }
    }
  }
  async getComments() {
    const { ctx, service } = this
    const { slug } = ctx.params
    const rule = {
      slug: { type: 'string', required: false }
    };
    ctx.validate(rule);
    const result = await service.comment.getComments()
    if(result) {
      ctx.body = ctx.showSuccess(result)
    } else {
      ctx.body = {
        code: ctx.errorMap.getCommentFail.code,
        message: ctx.errorMap.getCommentFail.message,
      }
    }
  }
  async deleteComment() {
    const { ctx, service } = this
    const { id, slug } = ctx.params
    const rule = {
      id: { type: 'string', required: false },
      slug: { type: 'string', required: false }
    };
    ctx.validate(rule);
    const result = await service.comment.deleteComment()
    if(result.n === 1 && result.deletedCount === 1) {
      ctx.body = ctx.showSuccess(result)
    } else {
      ctx.body = {
        code: ctx.errorMap.deleteCommentFail.code,
        message: ctx.errorMap.deleteCommentFail.message,
      }
    }
  }
  async favoriteArticle() {
    console.log('favoriteArticle contoller')
    // 文章已经被点赞过了
    const { ctx, service } = this
    const { slug } = ctx.params;
    const rule = {
      slug: { type: 'string', required: false }
    };
    ctx.validate(rule);
    let result = await service.article.favoriteArticle()
    if(result) {
      ctx.body = ctx.showSuccess(result)
    } else {
      ctx.body = {
        code: ctx.errorMap.articleFavoritedFail.code,
        message: ctx.errorMap.articleFavoritedFail.message,
      }
    }
  }
  async unfavoriteArticle() {
    const { ctx, service } = this
    const { slug } = ctx.params;
    const rule = {
      slug: { type: 'string', required: false }
    };
    ctx.validate(rule);
    let result = await service.article.unfavoriteArticle()
    if(result.n === 1 && result.deletedCount === 1) {
      ctx.body = ctx.showSuccess(result)
    } else {
      ctx.body = {
        code: ctx.errorMap.articleUnFavoritedFail.code,
        message: ctx.errorMap.articleUnFavoritedFail.message,
      }
    }
  }
}

module.exports = ArticleController;